<template>
  <a-card :bordered='false'>
    <div style='display: flex; justify-content: space-between'>
      <!-- 操作按钮区域 -->
      <div class='table-operator'>
        <a-button @click='handleAdd' type='primary' icon='plus' v-has="'certificate:add'">新增</a-button>
        <!-- <a-button type="primary" icon="download" @click="handleExportXls('优惠券表')">导出</a-button> -->
        <!-- <a-upload
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="importExcelUrl"
        @change="handleImportExcel"
      >
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload> -->
        <a-dropdown v-if='selectedRowKeys.length > 0'>
          <a-menu slot='overlay'>
            <a-menu-item key='1' @click='batchDel'
            >
              <a-icon type='delete' v-has="'certificate:delete'" />
              删除
            </a-menu-item
            >
          </a-menu>
          <a-button style='margin-left: 8px'> 批量操作
            <a-icon type='down' />
          </a-button>
        </a-dropdown>
      </div>
      <!-- 查询区域 -->
      <div class='table-page-search-wrapper'>
        <a-form layout='inline' @keyup.enter.native='searchQuery'>
          <a-row :gutter='24'>
            <a-col :xl='6' :lg='7' :md='8' :sm='24'>
              <a-form-item label='最低消费金额'>
                <a-input placeholder='请输入最低消费金额' v-model='queryParam.minimumAmount'></a-input>
              </a-form-item>
            </a-col>
            <template>
              <a-col :xl='4' :lg='7' :md='8' :sm='24'>
                <a-form-item label='券标题'>
                  <a-input placeholder='请输入券标题' v-model='queryParam.title'></a-input>
                </a-form-item>
              </a-col>
              <a-col :xl='6' :lg='7' :md='8' :sm='24'>
                <a-form-item label='优惠金额'>
                  <a-input placeholder='请输入优惠金额' v-model='queryParam.preferentialAmount'></a-input>
                </a-form-item>
              </a-col>
            </template>
            <a-col :xl='8' :lg='7' :md='8' :sm='24'>
              <span style='float: left; overflow: hidden' class='table-page-search-submitButtons'>
                <a-button type='primary' @click='searchQuery' icon='search'>查询</a-button>
                <a-button type='primary' @click='searchReset' icon='reload' style='margin-left: 8px'>重置</a-button>
<!--                <a-button type='primary' icon='download' @click="handleExportXls('优惠券表')" style='margin-left: 8px'-->
<!--                >导出</a-button-->
<!--                >-->
<!--                <a-upload-->
<!--                  name='file'-->
<!--                  :showUploadList='false'-->
<!--                  :multiple='false'-->
<!--                  :headers='tokenHeader'-->
<!--                  :action='importExcelUrl'-->
<!--                  @change='handleImportExcel'-->
<!--                  style='margin-left: 8px'-->
<!--                >-->
<!--                  <a-button type='primary' icon='import'>导入</a-button>-->
<!--                </a-upload>-->
                <!-- <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a> -->
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class='ant-alert ant-alert-info' style='margin-bottom: 16px'>
        <i class='anticon anticon-info-circle ant-alert-icon'></i> 已选择
        <a style='font-weight: 600'>{{ selectedRowKeys.length }}</a
        >项
        <a style='margin-left: 24px' @click='onClearSelected'>清空</a>
      </div>

      <a-table
        ref='table'
        size='middle'
        bordered
        rowKey='id'
        :columns='columns'
        :dataSource='dataSource'
        :pagination='ipagination'
        :loading='loading'
        class='j-table-force-nowrap'
        :rowSelection='{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }'
        @change='handleTableChange'
      >
        <span slot='action' slot-scope='text, record'>
          <a @click='handleEdit(record)' v-has="'certificate:edit'">编辑</a>
          <a-divider type='vertical' v-has="'certificate:edit'" />
          <a @click='handleCoupon(record.id)' v-has="'certificate:allocation'">分配优惠券</a>
           <a-divider type='vertical' v-has="'certificate:edit'" />


<!--          <a-popconfirm title='确定给全部用户分配优惠卷吗？' @confirm='handleRecall(record)'>-->
<!--           <a @click='allSend(record.id)' v-has="'certificate:allSend'">一键分配</a>-->
<!--          </a-popconfirm>-->

          <a-popconfirm title="确定给全部用户分配优惠券吗？" @confirm="allSend(record.id)">
          <a v-has="'certificate:allSend'">一键分配</a>
          <a-button type="primary" slot="confirm">确认</a-button>
          </a-popconfirm>


          <a-divider type='vertical' v-has="'certificate:allocation'" />
          <a-dropdown v-has="'certificate:delete'">
            <a class='ant-dropdown-link'>更多 <a-icon type='down' /></a>
            <a-menu slot='overlay'>
              <a-menu-item>
                <a-popconfirm title='确定删除吗?' @confirm='() => handleDelete(record.id)'>
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <xhdjPreferentialCertificate-modal ref='modalForm' @ok='modalFormOk'></xhdjPreferentialCertificate-modal>
    <certificateCoupon ref='dialogForm' @ok='modalFormOk'></certificateCoupon>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import XhdjPreferentialCertificateModal from './modules/XhdjPreferentialCertificateModal'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import certificateCoupon from './modules/XhdjPreferentialCertificateCoupon.vue'
import { httpAction } from '@/api/manage'

export default {
  name: 'XhdjPreferentialCertificateList',
  mixins: [JeecgListMixin],
  components: {
    XhdjPreferentialCertificateModal,
    certificateCoupon
  },
  data() {
    return {
      description: '优惠券表管理页面',
      isorter: {
        column: null,
        order: 'desc'
      },
      // 表头
      columns: [
        {
          title: '优惠卷编号',
          align: 'center',
          dataIndex: 'id'
        },
        // {
        //   title: '服务商编码',
        //   align: 'center',
        //   dataIndex: 'agencyCode'
        // },
        {
          title: '卷类型标题',
          align: 'center',
          dataIndex: 'preferentialName'
        },
        {
          title: '最低消费金额',
          align: 'center',
          dataIndex: 'minimumAmount'
        },
        {
          title: '券标题',
          align: 'center',
          dataIndex: 'title'
        },
        {
          title: '优惠金额',
          align: 'center',
          dataIndex: 'preferentialAmount'
        },
        // {
        //   title: '指定服务id',
        //   align: 'center',
        //   dataIndex: 'serverIds'
        // },
        {
          // 1-不限日期  2-指定到期时间 3-领取后计算到期时间
          title: '优惠券类型 ',
          align: 'center',
          dataIndex: 'preferentialType',
          customRender: (text, record) => {
            return record.preferentialType == 1
              ? '不限日期'
              : record.preferentialType == 2
                ? '指定到期时间未知'
                : '领取后计算到期时间'
          }
        },
        {
          title: '到期时间',
          align: 'center',
          dataIndex: 'endTime'
        },
        {
          title: '领取后到期天数',
          align: 'center',
          dataIndex: 'endDays'
        },
        // {
        //   // （1启用，0不启用）
        //   title: '状态',
        //   align: 'center',
        //   dataIndex: 'status'
        // },
        {
          title: '优惠券开始使用时间',
          align: 'center',
          dataIndex: 'startTime'
        },
        {
          title: '是否新人卷',
          align: 'center',
          dataIndex: 'isNew',
          customRender: (text, record) => {
            return record.isNew === 1
              ? '是'
              : '否'
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/xhdjPreferentialCertificate/list',
        delete: '/xhdjPreferentialCertificate/delete',
        deleteBatch: '/xhdjPreferentialCertificate/deleteBatch',
        exportXlsUrl: 'xhdjPreferentialCertificate/exportXls',
        importExcelUrl: 'xhdjPreferentialCertificate/importExcel',
        allSend: '/xhdjPreferentialCertificate/allSend'

      }
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    handleCoupon(id) {
      this.$refs.dialogForm.edit(id)
      this.$refs.dialogForm.title = '优惠券分配'
      this.$refs.dialogForm.disableSubmit = false
    },
    allSend(id) {
      let data = {
        id: id
      }
      httpAction(this.url.allSend, data, 'post').then(res => {
        if (res.success) {
          this.$message.success(res.message)
          this.loadData()
        } else {
          this.$message.warning(res.message)
        }
      })
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';

.ant-col {
  display: flex;
  justify-content: flex-end;
}
</style>
